package com.wzs.easytobuy.service.impl;

import com.wzs.easytobuy.exception.LoginException;
import com.wzs.easytobuy.mapper.AddressMapper;
import com.wzs.easytobuy.mapper.UserMapper;
import com.wzs.easytobuy.model.Address;
import com.wzs.easytobuy.model.User;
import com.wzs.easytobuy.service.IUserService;
import com.wzs.easytobuy.utils.MD5Util;
import com.wzs.easytobuy.utils.UUIDUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author miaomiao
 * @version 1.0.0
 * @Description 处理与用户模块相关业务的接口实现类
 * @createTime 2021-06-23- 09:00:00
 */
@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private AddressMapper AddressMapper;

    @Override
    public User login(String username, String passwordMD5) throws LoginException {

        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", passwordMD5);

        User user = userMapper.login(map);

        if (user == null) {
            throw new LoginException("用户名或密码错误");
        }

        return user;
    }

    @Override
    public int register(User user) {

        String id = UUIDUtil.getUUID();
        String password = MD5Util.getMD5(user.getPassword());
        user.setId(id);
        user.setPassword(password);

        return userMapper.register(user);
    }

    @Override
    public int checkUserName(String username) {
        return userMapper.checkUserName(username);
    }

    @Override
    public int checkEmail(String email) {
        return userMapper.checkEmail(email);
    }

    @Override
    public int addAddress(User user) {

        String userId = user.getId();
        String email = user.getEmail();
        String phone = user.getPhone();
        String id = UUIDUtil.getUUID();

        Address address = new Address();

        address.setId(id);
        address.setPhone(phone);
        address.setEmail(email);
        address.setUserId(userId);
        address.setAddress(" ");
        address.setFlag("1");
        address.setPostCode(0);
        address.setFullName(" ");


        return AddressMapper.insertSelective(address);
    }
}





















